{% from "../../macros/attributes.njk" import govukAttributes -%}

{% set blockLevel = not params.items and (params.next or params.previous) -%}

{%- set arrowPrevious -%}
  <svg class="govuk-pagination__icon govuk-pagination__icon--prev" xmlns="http://www.w3.org/2000/svg" height="13" width="15" aria-hidden="true" focusable="false" viewBox="0 0 15 13">
    <path d="m6.5938-0.0078125-6.7266 6.7266 6.7441 6.4062 1.377-1.449-4.1856-3.9768h12.896v-2h-12.984l4.2931-4.293-1.414-1.414z"></path>
  </svg>
{%- endset -%}

{%- set arrowNext -%}
  <svg class="govuk-pagination__icon govuk-pagination__icon--next" xmlns="http://www.w3.org/2000/svg" height="13" width="15" aria-hidden="true" focusable="false" viewBox="0 0 15 13">
    <path d="m8.107-0.0078125-1.4136 1.414 4.2926 4.293h-12.986v2h12.896l-4.1855 3.9766 1.377 1.4492 6.7441-6.4062-6.7246-6.7266z"></path>
  </svg>
{%- endset -%}

{%- macro _arrowLink(link, type = "next") %}
  {% set arrowType = arrowPrevious if type == "prev" else arrowNext %}
  <div class="govuk-pagination__{{ type }}">
    <a class="govuk-link govuk-pagination__link" href="{{ link.href }}" rel="{{ type }}"
      {{- govukAttributes(link.attributes) }}>
      {% if blockLevel or type == "prev" %}
        {{- arrowType | safe | indent(4, true) }}
      {% endif %}
      <span class="govuk-pagination__link-title {%- if blockLevel and not link.labelText %} govuk-pagination__link-title--decorated{% endif %}">
        {{ caller() | safe | trim }}
      </span>
      {% if link.labelText and blockLevel %}
      <span class="govuk-visually-hidden">:</span>
      <span class="govuk-pagination__link-label">{{ link.labelText }}</span>
      {% endif %}
      {% if not blockLevel and type == "next" %}
        {{- arrowType | safe | indent(4, true) }}
      {% endif %}
    </a>
  </div>
{% endmacro -%}

{%- macro _pageItem(item) -%}
  <li class="govuk-pagination__item {%- if item.current %} govuk-pagination__item--current{% endif %} {%- if item.ellipsis %} govuk-pagination__item--ellipsis{% endif %}">
  {% if item.ellipsis %}
    &ctdot;
  {% else %}
    <a class="govuk-link govuk-pagination__link" href="{{ item.href }}" aria-label="{{ item.visuallyHiddenText | default("Page " + item.number) }}"
      {%- if item.current %} aria-current="page"{% endif %}
      {{- govukAttributes(item.attributes) }}>
      {{ item.number }}
    </a>
  {% endif %}
  </li>
{%- endmacro -%}

<nav class="govuk-pagination {%- if blockLevel %} govuk-pagination--block{% endif %} {%- if params.classes %} {{ params.classes }}{% endif %}" aria-label="{{ params.landmarkLabel | default("Pagination", true) }}"
  {{- govukAttributes(params.attributes) }}>
  {% set previous = params.previous %}
  {% set next = params.next %}

  {%- if previous and previous.href %}
    {% call _arrowLink(previous, "prev") %}
      {% if previous.html or previous.text %}
        {{ previous.html | safe | trim | indent(8) if previous.html else previous.text }}
      {% else %}
        Previous<span class="govuk-visually-hidden"> page</span>
      {% endif %}
    {% endcall %}
  {%- endif %}

  {%- if params.items %}
  <ul class="govuk-pagination__list">
  {% for item in params.items %}
    {% if item is not null and (item | length) > 0 %}
      {{ _pageItem(item) | indent(2) }}
    {% endif %}
  {% endfor %}
  </ul>
  {% endif %}

  {%- if next and next.href %}
    {% call _arrowLink(next, "next") %}
      {% if next.html or next.text %}
        {{ next.html | safe | trim | indent(8) if next.html else next.text }}
      {% else %}
        Next<span class="govuk-visually-hidden"> page</span>
      {% endif %}
    {% endcall %}
  {% endif %}
</nav>
